package cn.biplam.front.template.control;

import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.client.Operator;
import cn.biplam.common.opcode.SysManageCode;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.sqlresult.SqlResult;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlImage;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Position_Mode;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.template.base.Text;

/**
 * 滑动菜单
 */
public class SlipMenu {

	/**菜单页面容器ID */
	private String cID;
	/**网站主目录路径 */
	private String sHomeUrl;
	/**菜单数据源 */
	private DbResource dbr;
	/**菜单表名 */
	private String tableName;
	/**绘制中计算出的菜单项最大页面宽度 */
	private int maxLength;
	/**树的左键目标页面容器ID */
	private String target;
	/**菜单根节点id */
	private String nodeId;
	/**需要默认显示的节点id */
	private String defaultNodeId;
	private UserInfo mUser;

	/**
	 * 构造方法
	 * @param cID			菜单的页面容器ID
	 * @param tableName		菜单表名称
	 * @param sNodeId		根节点ID
	 * @param targetCID		菜单点击目标容器ID
	 * @param dbr			菜单所在数据源
	 * @param sHomeUrl		主页URL
	 * @param mUser			用户信息类
	 */
	public SlipMenu(String cID,String tableName,String sNodeId,
			String targetCID,DbResource dbr,String sHomeUrl,UserInfo mUser){
		this.cID = cID;
		this.target = targetCID;
		this.nodeId = sNodeId;
		this.tableName = tableName;
		this.dbr = dbr;
		this.sHomeUrl = sHomeUrl;
		this.mUser = mUser;
	}

	/**
	 * 返回滑动菜单的HtmlWindow对象
	 * @return HtmlDiv
	 * @throws ErrorMessage
	 */
	public HtmlWindow get() throws ErrorMessage{
		/**树的容器 */
		HtmlDiv div = new HtmlDiv(this.cID,"sleek");


		/**绘制滑动菜单树的页面对象*/
		this.putMenuSlip(div,this.nodeId);

		/**客户端绑定滑动脚本*/
		div.bind("sheekShow($('"+this.defaultNodeId+"'));");
		div.bind("mb('"+this.cID+"');");

		return div;
	}

	/**
	 * 向页面容器中放入滑动菜单页面对象
	 * @param div		页面容器
	 * @param db		数据库连接
	 * @param nodeId	当前层节点
	 * @throws ErrorMessage 
	 */
	private void putMenuSlip(HtmlDiv div , String nodeId) throws ErrorMessage {
		SqlResult rs = this.getResult(nodeId);
		/**循环绘制一级菜单*/
		while(rs.next()){
			String menuId=rs.getString(MenuStruc.MENU_ID);
			String baseID = this.cID +","+menuId;
			String itemContentID=baseID+",ct";

			//菜单栏页面容器
			HtmlDiv node = new HtmlDiv(null,"sleeko");
			if(this.defaultNodeId == null){//记录第一个菜单栏为默认菜单栏
				this.defaultNodeId = itemContentID;
			}

			//菜单栏正文容器
			HtmlDiv nodect = new HtmlDiv(itemContentID,"sleekm");
			nodect.put(this.getMenuPic());
			nodect.put(this.getLevel1Node(rs));
			nodect.bind(Window_Event.OnClick, "sheekShow(this)");

			//菜单栏下级容器
			HtmlDiv nodecset = new HtmlDiv(baseID+",cset","sleeki");

			node.put(nodect);
			node.put(nodecset);
			div.put(node);

			//绘制下级
			putMenuItem(nodecset, menuId);
		}
	}

	private void putMenuItem(HtmlDiv div , String nodeId) throws ErrorMessage {
		SqlResult rs = this.getResult(nodeId);
		/**循环绘制二级菜单*/
		while(rs.next()){
			div.put(this.getLevel2Node(rs));
		}
	}


	private SqlResult getResult(String nodeId)throws ErrorMessage{

		DataFilter filter= new DataFilter(nodeId);
		filter.put("tableName", this.tableName);

		SqlResult rs = Operator.get(SysManageCode.MenuOptCode.Query_UsersMenu, 
				this.mUser.getUserID(),
				filter,
				this.dbr
				).getSelectResult();

		return rs;
	}

	/**
	 * 返回一级菜单项文本的页面容器
	 * @param rs	菜单项查询结果
	 * @return	HtmlDiv
	 * @throws ErrorMessage
	 */
	private HtmlDiv getLevel1Node(SqlResult rs) throws ErrorMessage{
		HtmlDiv node = new HtmlDiv();

		/**拼接树的节点说明的id,目前以tree_id,node_id的方式拼接*/
		StringBuilder sb = new StringBuilder();
		sb.append(this.cID);
		sb.append(",");
		sb.append(nodeId);
		String nodeName=rs.getString(MenuStruc.MENU_NAME);
		HtmlWindow mText = new Text(nodeName,sb.toString()).get();
		mText.setCssID("sleekt");
		this.setMaxLength(nodeName);
		node.put(mText);

		node.setPositionMode(Position_Mode.Absolute);
		node.setStyle("padding-top","2px");
		node.setStyle("padding-left","10px");
		return node;
	}

	/**
	 * 返回一级菜单项文本的页面容器
	 * @param rs	菜单项查询结果
	 * @return	HtmlDiv
	 * @throws ErrorMessage
	 */
	private HtmlDiv getLevel2Node(SqlResult rs) throws ErrorMessage{
		HtmlDiv node = new HtmlDiv();
		String menuId=rs.getString(MenuStruc.MENU_ID);

		HtmlWindow pt = new PicText(
				rs.getString(MenuStruc.MENU_NAME),
				this.cID+","+menuId,
				this.sHomeUrl,
				rs.getString(MenuStruc.PIC)
		).get();
		/**刷新菜单区域*/
		pt.bind(Window_Event.OnClick,
				AjxScript.getM(
						this.cID,
						rs.getString(MenuStruc.ACTIONID),
						menuId,
						this.target
				)+"moc(this);"
		);
		node.put(pt);

		return node;
	}

	/**
	 * 设置菜单项的最大宽度
	 */
	private void setMaxLength(String text) throws ErrorMessage{
		int base=1;
		int length = 18*base+TOOLS.getTextLength(text);
		if(length>this.maxLength){
			this.maxLength=length;
		}
	}

	/**
	 * 菜单项的最大宽度
	 * @return int
	 */
	public int getNodeMaxLength(){
		return this.maxLength;
	}

	/**
	 * 取菜单背景图片
	 * @return
	 * @throws ErrorMessage 
	 */
	private HtmlWindow getMenuPic() throws ErrorMessage{
		HtmlWindow pic = new HtmlImage(this.sHomeUrl+"/images/title_bg.gif");
		pic.setPositionMode(Position_Mode.Absolute);
		pic.setStyleWidth("214");
		pic.setStyleHeight("25");
		pic.setStyle("padding-bottom","1px");
		return pic;
	}


	public static class MenuStruc{
		public static final String MENU_ID="MENU_ID";
		public static final String MENU_PARENT="MENU_PARENT";
		public static final String MENU_NAME="MENU_NAME";
		public static final String MENU_SEQUENCE="MENU_SEQUENCE";
		public static final String PIC="PIC";
		public static final String ACTIONID="ACTIONID";
	}
}
